package com.nsgf.library.ui.page.manager;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.kunminx.architecture.ui.page.DataBindingConfig;
import com.nsgf.library.BR;
import com.nsgf.library.R;
import com.nsgf.library.data.bean.UserInfo;
import com.nsgf.library.data.bean.WorkOrderDetailBean;
import com.nsgf.library.databinding.FragmentManagerWorkOrderAbnormalOpinionBinding;
import com.nsgf.library.domain.request.AccountRequester;
import com.nsgf.library.ui.page.BaseFragment;
import com.nsgf.library.ui.page.StateHolder;
import com.nsgf.library.utils.Kits;
import com.nsgf.library.utils.StringUtil;
import com.nsgf.library.widget.prckerview.OptionsPickerView;

import java.util.ArrayList;
import java.util.Calendar;

/**
 * 审批意见
 */
public class MessageAbnormalOpinionFragment extends BaseFragment {

    public static MessageAbnormalOpinionFragment newInstance(int isResponsibleBy) {
        MessageAbnormalOpinionFragment messageFragment = new MessageAbnormalOpinionFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("isResponsibleBy", isResponsibleBy);
        messageFragment.setArguments(bundle);
        return messageFragment;
    }

    public static MessageAbnormalOpinionFragment newInstance(int isResponsibleBy, WorkOrderDetailBean.DataBean dataBean) {
        MessageAbnormalOpinionFragment messageFragment = new MessageAbnormalOpinionFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("isResponsibleBy", isResponsibleBy);
        bundle.putParcelable("dataBean", dataBean);
        messageFragment.setArguments(bundle);
        return messageFragment;
    }

    private WorkOrderDetailBean.DataBean getDataBean() {
        return getArguments().getParcelable("dataBean");
    }

    private AccountRequester mAccountRequester;

    private int getResponsibleBy() {
        return getArguments().getInt("isResponsibleBy", 0);
    }

    StateHolder mStates;


    public FragmentManagerWorkOrderAbnormalOpinionBinding getmBinding() {
        return mBinding;
    }

    @Override
    protected void initViewModel() {
        mStates = getActivityScopeViewModel(StateHolder.class);
        mAccountRequester = getActivityScopeViewModel(AccountRequester.class);
    }


    @Override
    protected DataBindingConfig getDataBindingConfig() {


        return new DataBindingConfig(R.layout.fragment_manager_work_order_abnormal_opinion, BR.vm, mStates)
                .addBindingParam(BR.click, new ClickProxy());
    }

    private FragmentManagerWorkOrderAbnormalOpinionBinding mBinding;

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mBinding = (FragmentManagerWorkOrderAbnormalOpinionBinding) getBinding();
        getLifecycle().addObserver(mAccountRequester);
        Calendar calendar = Calendar.getInstance();
        mStates.time.set(Kits.Date.getYmdhmss(calendar.getTimeInMillis()));

        if (0 == getResponsibleBy()) {
            mBinding.etOpinionConfirmOpinions.setEnabled(false);
            mBinding.tvAbnormalEffect.setEnabled(false);
            mStates.workOrderDetailBean.set(getDataBean());
            mStates.workOrderDetailBean.get().setApprovalTime(StringUtil.isEmpty(mStates.workOrderDetailBean.get().getApprovalTime()) ? "--" :
                    mStates.workOrderDetailBean.get().getApprovalTime());
            mStates.workOrderDetailBean.get().setEffect(StringUtil.isEmpty(mStates.workOrderDetailBean.get().getEffect()) ? "--" :
                    mStates.workOrderDetailBean.get().getEffect());
            mStates.workOrderDetailBean.get().setConfirmOpinions(StringUtil.isEmpty(mStates.workOrderDetailBean.get().getConfirmOpinions()) ? " " :
                    mStates.workOrderDetailBean.get().getConfirmOpinions());
        } else {

            WorkOrderDetailBean.DataBean dataBean = getDataBean();
            if (!StringUtil.isEmpty(dataBean.getEffect())){
                mBinding.tvAbnormalEffect.setEnabled(false);
                mBinding.etOpinionConfirmOpinions.setEnabled(false);
            }
            dataBean.setResponsibleBy(UserInfo.getInstance().getName());
            dataBean.setApprovalTime(mStates.time.get());
            mStates.workOrderDetailBean.set(dataBean);

        }


        mBinding.etOpinionConfirmOpinions.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                // 当文本改变时更新长度显示
                int length = charSequence.length();
                mBinding.textView.setText(length + "/500");

            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    public class ClickProxy {

        public void screenType() {
            final ArrayList<String> list = new ArrayList<>();
            list.add("很好");
            list.add("正常");
            list.add("一般");
            OptionsPickerView<String> mOptionsPickerView = new OptionsPickerView.Builder(getActivity(), new OptionsPickerView.OnOptionsSelectListener() {
                @Override
                public void onOptionsSelect(int options1, int options2, int options3, View v) {
                    mBinding.tvAbnormalEffect.setText(list.get(options1));
                }
            }).build();
            mOptionsPickerView.setPicker(list);
            mOptionsPickerView.show();
        }

    }
}
